package dto

import (
	"sort"
	"strconv"
	"strings"
	"time"
	"vrcm/src/vrcm/app/admin/models"
	"vrcm/src/vrcm/app/admin/models/subject"
	"vrcm/src/vrcm/common/dto"
	common "vrcm/src/vrcm/common/models"
)

type AnswerRecordGetPageReq struct {
	dto.Pagination `search:"-"`
	AnswerRecordOrder
}

type AnswerRecordOrder struct {
	Id         string `form:"idOrder"  search:"type:order;column:id;table:answer_record"`
	TenantId   string `form:"tenant_idOrder"  search:"type:order;column:tenant_id;table:answer_record"`
	EventId    string `form:"event_idOrder"  search:"type:order;column:event_id;table:answer_record"`
	Account    string `form:"accountOrder"  search:"type:order;column:account;table:answer_record"`
	Type       int    `form:"typeOrder"  search:"type:order;column:type;table:answer_record"`
	SubjectId  string `form:"subject_idOrder"  search:"type:order;column:subject_id;table:answer_record"`
	Answer     string `form:"answerOrder"  search:"type:order;column:answer;table:answer_record"`
	IsRight    int    `form:"is_rightOrder"  search:"type:order;column:is_right;table:answer_record"`
	CreateTime int64  `form:"create_timeOrder"  search:"type:order;column:create_time;table:answer_record"`
	UpdateTime int64  `form:"update_timeOrder"  search:"type:order;column:update_time;table:answer_record"`
	CreateBy   string `form:"createByOrder"  search:"type:order;column:create_by;table:answer_record"`
	UpdateBy   string `form:"updateByOrder"  search:"type:order;column:update_by;table:answer_record"`
}

func (m *AnswerRecordGetPageReq) GetNeedSearch() interface{} {
	return *m
}

type AnswerRecordInsertReq struct {
	Id        int      `json:"-" comment:"id"` // id
	TenantId  string   `json:"tenant_id" comment:"租户id" validate:"required"`
	EventId   string   `json:"event_id" comment:"活动id" validate:"required,max=35"`
	Account   string   `json:"account" comment:"嘉宾账户" validate:"required,max=50"`
	Type      int      `json:"type" comment:"类型 1.题目 2.投票 3.题词 4.点赞" validate:"required,gt=0,lte=4"`
	SubjectId int      `json:"subject_id" comment:"题目id" validate:"required"`
	PaperId   int      `json:"paper_id" comment:"试卷id" validate:"required"`
	Answer    []string `json:"answer" comment:"答案" validate:"required,max=200"`
	common.ControlBy
}

func (s *AnswerRecordInsertReq) Generate(model *subject.AnswerRecord) {
	if s.Id == 0 {
		model.Model = common.Model{Id: s.Id}
	}
	now := time.Now().Unix()
	model.TenantId = s.TenantId
	model.EventId = s.EventId
	model.Account = s.Account
	model.Type = s.Type
	model.SubjectId = s.SubjectId
	model.PaperId = s.PaperId
	model.IsDelete = 2
	model.CreateTime = now
	model.UpdateTime = now
	model.CreateBy = s.CreateBy // 添加这而，需要记录是被谁创建的
	if len(s.Answer) > 0 {
		//答案做排序处理
		sort.Slice(s.Answer, func(i, j int) bool {
			n1, _ := strconv.Atoi(s.Answer[i])
			n2, _ := strconv.Atoi(s.Answer[j])
			return n1 < n2
		})
		tempAnswers := strings.Join(s.Answer, models.Delimiter)
		model.Answer = tempAnswers
	}
}

func (s *AnswerRecordInsertReq) GetId() interface{} {
	return s.Id
}

type AnswerRecordUpdateReq struct {
	Id         int    `uri:"id" comment:"id"` // id
	TenantId   string `json:"tenant_id" comment:"租户id"`
	EventId    string `json:"event_id" comment:"活动id"`
	Account    string `json:"account" comment:"嘉宾账户"`
	Type       int    `json:"type" comment:"类型 1.题目 2.投票 3.题词 4.点赞"`
	SubjectId  int    `json:"subject_id" comment:"题目id"`
	PaperId    int    `json:"paper_id" comment:"试卷id"`
	Answer     string `json:"answer" comment:"答案"`
	IsRight    int    `json:"is_right" comment:"是否正确 1正确 2错误"`
	CreateTime int64  `json:"create_time" comment:"创建时间"`
	UpdateTime int64  `json:"update_time" comment:"更新时间"`
	common.ControlBy
}

func (s *AnswerRecordUpdateReq) Generate(model *subject.AnswerRecord) {
	if s.Id == 0 {
		model.Model = common.Model{Id: s.Id}
	}
	model.TenantId = s.TenantId
	model.EventId = s.EventId
	model.Account = s.Account
	model.Type = s.Type
	model.SubjectId = s.SubjectId
	model.PaperId = s.PaperId
	model.Answer = s.Answer
	model.IsRight = 2
	model.CreateTime = s.CreateTime
	model.UpdateTime = s.UpdateTime
	model.UpdateBy = s.UpdateBy // 添加这而，需要记录是被谁更新的
}

func (s *AnswerRecordUpdateReq) GetId() interface{} {
	return s.Id
}

// AnswerRecordGetReq 功能获取请求参数
type AnswerRecordGetReq struct {
	Id        int    `uri:"id"`
	TenantId  string `json:"tenant_id" comment:"租户id" validate:"required"`
	EventId   string `json:"event_id" comment:"活动id" validate:"required"`
	Account   string `json:"account" comment:"嘉宾账户" validate:"required,max=50"`
	SubjectId int    `json:"subject_id" comment:"题目id"  validate:"required"`
	PaperId   int    `json:"paper_id" comment:"试卷id" validate:"required"`
}

func (s *AnswerRecordGetReq) GetId() interface{} {
	return s.Id
}

// AnswerRecordDeleteReq 功能删除请求参数
type AnswerRecordDeleteReq struct {
	Ids []int `json:"ids"`
}

func (s *AnswerRecordDeleteReq) GetId() interface{} {
	return s.Ids
}
